package com.bluesky.jwf.component.select;

import java.util.ArrayList;
import java.util.List;

import com.bluesky.javawebbrowser.domain.html.tags.form.select.Option;
import com.bluesky.javawebbrowser.domain.html.tags.form.select.Select;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.event.EventHandler;

public class ListBox extends Component implements MultipleSelector {
	private static String EVENT_CHANGE = "change";

	private Select selectTag;
	private Option optionTag;

	private TextConverter textConverter;
	private List items;

	// visible list rows
	private int size;

	public ListBox() {
		items = new ArrayList();

		// set default converter
		textConverter = new TextConverter() {
			@Override
			public String toText(Object obj) {

				return obj.toString();
			}
		};
	}

	@Override
	public void init(String html) {

		super.init(html);

		selectTag = (Select) loadByJwfClass("select");
		optionTag = (Option) selectTag.loadByJwfClass("option");

		
		//make sure
		selectTag.setMultiple(true);
	}

	@Override
	public int[] getSelectedIndices() {
		return selectTag.getSelectedIndices();

	}

	@Override
	public List getSelectedItems() {
		int[] indices = selectTag.getSelectedIndices();

		List result = new ArrayList();
		for (int i : indices)
			result.add(items.get(i));

		return result;
	}

	@Override
	public void setItems(List items) {
		this.items = items;

		selectTag.clear();
		int i = 0;
		for (Object o : items) {
			String label = textConverter.toText(o);
			selectTag.addOption(String.valueOf(i), label);

			i++;
		}

	}

	@Override
	public void setSelectedIndices(int[] selectedIndices) {
		selectTag.setSelectedIndices(selectedIndices);

	}

	@Override
	public void setSelectedItems(List items) {
		List<Integer> list = new ArrayList<Integer>();
		for (Object o : items) {
			int i = this.items.indexOf(o);
			if (i >= 0)
				list.add(i);
		}

		int[] indices = new int[list.size()];
		for (int i = 0; i < list.size(); i++)
			indices[i] = list.get(i);

		setSelectedIndices(indices);

	}

	@Override
	public void setId(String id) {
		super.setId(id);
		selectTag.setName(getId() + "_select");

	}

	@Override
	public void setPostBackData(String name, String value) {

		super.setPostBackData(name, value);

		if (name.equals("select"))
			selectTag.setHttpValue(value);

	}

	public void setChangeHandler(EventHandler handler) {

		if (handler != null) {
			selectTag.setOnChange(String.format(
					"_oum_post(this,'%s','change')", getId()));
		} else
			selectTag.setOnChange("");

		eventHandlers.put(EVENT_CHANGE, handler);
	}

	public TextConverter getTextConverter() {
		return textConverter;
	}

	public void setTextConverter(TextConverter textConverter) {
		this.textConverter = textConverter;
	}

	public Integer getSize() {
		return selectTag.getSize();
	}

	public void setSize(Integer size) {
		selectTag.setSize(size);

	}

}
